package com.baichang.taoshunda.controller.mobile.business;

import com.baichang.framework.controller.WebBaseController;
import com.baichang.framework.excelption.ServiceException;
import com.baichang.taoshunda.controller.ControllerConstants;
import com.baichang.taoshunda.entity.Html;
import com.baichang.taoshunda.service.CommonProblemService;
import com.baichang.taoshunda.service.HtmlService;
import com.baichang.taoshunda.service.RedPacketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(ControllerConstants.BUSSINESSWEBVIEW)
public class WebViewMController extends WebBaseController {

    @Autowired
    private HtmlService htmlService;

    @Autowired
    private CommonProblemService commonProblemService;

    @Autowired
    private RedPacketService redPacketService;

    /**
     * 关于我们
     *
     * @param request
     * @param response
     */
    @RequestMapping("/constantsUs")
    public String constantsUs(HttpServletRequest request, HttpServletResponse response) {

        return "";
    }

    /**
     * 用户协议
     *
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/userAgreement")
    public String userAgreement(HttpServletRequest request, HttpServletResponse response, Model model) {
        String content = "";
        try {
            Html html = htmlService.queryById(2);
            content = html.getValue();
        } catch (ServiceException e) {
            content = "";
        }
        model.addAttribute("content", content);
        return "taoshunda/businessAgreement";
    }

    /**
     * 商家端隐私
     *
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/companyPrivacy")
    public String companyPrivacy(HttpServletRequest request, HttpServletResponse response, Model model) throws ServiceException {
        Html content = htmlService.queryById(6);
        model.addAttribute("content", content.getValue());
        return "taoshunda/aboutUs";
    }

    /**
     * 消费者保证协议
     *
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/consumptionAgreement")
    public String businessAgreement(HttpServletRequest request, HttpServletResponse response, Model model) throws ServiceException {
        Html html = htmlService.queryById(4);
        model.addAttribute("content", html.getValue());
        return "taoshunda/consumptionAgreement";
    }

    /**
     * 邀请商家的连接跳转的页面
     *
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/register")
    public ModelAndView register(HttpServletRequest request, HttpServletResponse response, Model model) throws ServiceException {
        ModelAndView mv = new ModelAndView("taoshunda/register");
        mv.addObject("userId", request.getParameter("userId"));
        return mv;
    }

    /**
     * 消费者保证服务协议
     *
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/guaranteeAgreement")
    public String guaranteeAgreement(HttpServletRequest request, HttpServletResponse response, Model model) {
        String content = "";
        try {
            Html html = htmlService.queryById(5);
            content = html.getValue();
        } catch (ServiceException e) {
            content = "";
        }
        model.addAttribute("content", content);
        return "taoshunda/guaranteeAgreement";
    }

    /**
     * 查询常见问题
     *
     * @param request
     * @param response
     */
    @RequestMapping("/queryAnswer")
    public String queryAnswer(HttpServletRequest request, HttpServletResponse response, Model model) {
        List<Map<String, Object>> list = commonProblemService.queryAnswer(2);
        model.addAttribute("code", list);
        return "taoshunda/problemBus";
    }

    /**
     * 退店保证协议
     *
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/retreatAgreement")
    public String retreatAgreement(HttpServletRequest request, HttpServletResponse response, Model model) throws ServiceException {
        Html html = htmlService.queryById(8);
        model.addAttribute("content", html.getValue());
        return "taoshunda/retreatAgreement";
    }

    /**
     * 假一赔三协议
     *
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/falseAgreement")
    public String falseAgreement(HttpServletRequest request, HttpServletResponse response, Model model) throws ServiceException {
        Html html = htmlService.queryById(7);
        model.addAttribute("content", html.getValue());
        return "taoshunda/falseAgreement";
    }

    /**
     * 分享红包
     *
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/redPacket")
    public String redPacket(HttpServletRequest request, HttpServletResponse response, Model model) throws ServiceException {

        Integer redPacketId = ServletRequestUtils.getIntParameter(request, "redPacketId", 0);
        Map<String, Object> map = redPacketService.getRedPacketById(redPacketId, 0);
        String redMessage = getRedMessage(map);
        map.put("redMessage", redMessage);
        model.addAttribute("map", map);
        return "taoshunda/redPacket";
    }

    private String getRedMessage(Map<String, Object> map) {
        String redMessage;
        int timeLength = Integer.parseInt(map.get("timeLength").toString());
        int allNumber = Integer.parseInt(map.get("allNumber").toString());
        int number = Integer.parseInt(map.get("number").toString());
        String allMoney = map.get("allMoney").toString();
        if (timeLength < 60) {
            if (number == allNumber) {
                redMessage = allNumber + "个红包，总金额" + allMoney + "元，" + timeLength + "分钟被抢光";
            } else {
                redMessage = "已抢" + number + "/" + allNumber + "个红包，总金额" + allMoney + "元";
            }
        } else if (timeLength >= 60 && timeLength < 1440) {
            int h = timeLength / 60;
            int m = timeLength % 60;
            if (number == allNumber) {
                redMessage = allNumber + "个红包，总金额" + allMoney + "元，" + h + " 时" + m + "分钟被抢光";
            } else {
                redMessage = "已抢" + number + "/" + allNumber + "个红包，总金额" + allMoney + "元";
            }
        } else {
            int d = timeLength / 1440;
            int h = timeLength % 1440 / 60;
            int m = timeLength % 60;
            if (number == allNumber) {
                redMessage = allNumber + "个红包，总金额" + allMoney + "元，" + d + "天" + h + " 时" + m + "分钟被抢光";
            } else {
                redMessage = "已抢" + number + "/" + allNumber + "个红包，总金额" + allMoney + "元";
            }
        }
        return redMessage;
    }
}
